<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/3/3
 * Time: 下午4:30
 * 上门车用户
 */
class OdsUserController extends BaseController{
    /*
     * 上门车登录
     * */
    public function login(){
        $validator = Validator::make(Input::all(),
            array(
                'car_no' => 'required',
                'passwd' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $car_no = Input::get('car_no');
            $passwd = md5(Input::get('passwd'));

            //验证密码
            $account_info = MaintenanceEngineers::where('account',$car_no)
                ->where('type','car')
                ->where('passwd',$passwd)
                ->where('status',1)
                ->first();
            if(!is_null($account_info)){
                try {
                    $car_info = MaintenanceCars::where('id',$account_info->type_id)
                        ->where('status',1)
                        ->select(array('id','car_name','shop_id'))
                        ->first();

                    CarTokens::where('car_id',$car_info->id)->delete();
                    $car_token = new CarTokens;
                    $car_token->car_id = $car_info->id;
                    $car_token->token = md5($car_no.time().rand(0,100));
                    $car_token->created_at = Common::num2DateTime(time());
                    $car_token->out_time_at = Common::num2DateTime(time() + Config::get("ods_user.token_time_out"));
                    $car_token->save();
                } catch (Exception $e) {
                    Log::error("error", array("system" => "ods" ,"event" => "get_car_engineer", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("ods_error.login_create_token_error_12003");
                    break;
                }
            }else{
                $response = Lang::get("ods_error.car_engineer_not_exist_12002");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'car_id' => $car_info->id,
                    'car_name' => $car_info->name,
                    'car_no' => $car_no,
                    'shop_id' => $car_info->shop_id,
                    'token' => $car_token->token,
                    'expired' => $car_token->out_time_at
                )
            );
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "login", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 上门车登出
     * */
    public function logout(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');

            //验证token
            $car_info = CarTokens::where('token',$token)->select(array('car_id'))->first();
            if(!is_null($car_info)){
                try{
                    CarTokens::where('car_id',$car_info->car_id)->delete();
                } catch (Exception $e) {
                    Log::error("error", array("system" => "ods" ,"event" => "logout", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("ods_error.user_token_clean_error_12004");
                    break;
                }
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array('car_id' => 0)
            );

        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "logout", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}